--[[
    队列
    先进先出
    基本操作
    1.清空队列
    2.入队
    3.出队
    4.取节点个数

--]]

local table_insert = table.insert
local table_remove = table.remove

local Queue = {}

function Queue.new(...)
    local instance = setmetatable({}, {__index=Queue})
    instance:ctor(...)
    return instance
end

function Queue:ctor()
    self.head = 0 --标记
    self.tail = 1 --标记
    self.data = {} --用于存放数据
    self.count = 0
    self:clear()
end

function Queue:push(node)
    self.tail = self.tail - 1
    self.data[self.tail] = node
    self.count = self.count + 1
end

function Queue:pop()
    if self.head < self.tail then
        return
    end
    local value = self.data[self.head]
    self.data[self.head] = nil
    self.head = self.head - 1
    self.count = self.count - 1
    return value
end

function Queue:getData()
    return self.data
end

function Queue:print()
    for i = self.tail, self.head do
        print(i, self.data[i])
    end
end

function Queue:size()
    return self.count
end

function Queue:clear()
    self.head = 0 --标记
    self.tail = 1 --标记
    self.data = {} --用于存放数据
    self.count = 0
end

------------------------------------------------------------------
--测试
------------------------------------------------------------------
if arg and arg[0] == "queue.lua" then
    local queue = Queue.new()
    queue:push(1)
    queue:push(2)
    queue:push(3)
    queue:push(4)
    queue:push(5)
    queue:print()
    print("_______")

    local a = queue:pop()
    local b =queue:pop()
    print("pop:", a, b)
    print("_______")
    queue:print()
    queue:clear()
    print("_______")
    queue:print()
end

return Queue